import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton, QVBoxLayout, QMessageBox
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFont
import sqlite3
import hashlib

def hash_password(password):
    """使用 SHA-256 哈希函数加密密码。"""
    sha_signature = hashlib.sha256(password.encode()).hexdigest()
    return sha_signature

class LoginWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('登录界面')
        self.setGeometry(300, 300, 300, 250)  # 设置窗口位置和大小

        font_large = QFont('Arial', 12)  # 字体和大小

        layout = QVBoxLayout()

        self.label_username = QLabel('用户名:', self)
        self.label_username.setFont(font_large)
        layout.addWidget(self.label_username)

        self.entry_username = QLineEdit(self)
        self.entry_username.setFont(font_large)
        self.entry_username.setFixedSize(250, 30)  # 输入框大小
        layout.addWidget(self.entry_username)

        self.label_password = QLabel('密码:', self)
        self.label_password.setFont(font_large)
        layout.addWidget(self.label_password)

        self.entry_password = QLineEdit(self)
        self.entry_password.setEchoMode(QLineEdit.Password)
        self.entry_password.setFont(font_large)
        self.entry_password.setFixedSize(250, 30)  # 输入框大小
        layout.addWidget(self.entry_password)

        self.button_login = QPushButton('登录', self)
        self.button_login.setFont(font_large)
        self.button_login.setFixedSize(100, 30)  # 按钮大小
        self.button_login.clicked.connect(self.login)
        layout.addWidget(self.button_login)

        self.button_register = QPushButton('注册', self)
        self.button_register.setFont(font_large)
        self.button_register.setFixedSize(100, 30)  # 按钮大小
        self.button_register.clicked.connect(self.register)
        layout.addWidget(self.button_register)

        layout.setAlignment(Qt.AlignCenter)  # 设置布局中心对齐
        self.setLayout(layout)

        self.conn = sqlite3.connect('user.db')
        self.cursor = self.conn.cursor()

    def login(self):
        username = self.entry_username.text()
        password = hash_password(self.entry_password.text())
        self.cursor.execute('SELECT * FROM users WHERE username=? AND password=?', (username, password))
        if self.cursor.fetchone():
            QMessageBox.information(self, '登录成功', '你已成功登录。')
            self.close()
        else:
            QMessageBox.critical(self, '登录失败', '用户名或密码错误')

    def register(self):
        username = self.entry_username.text()
        password = hash_password(self.entry_password.text())
        try:
            self.cursor.execute('INSERT INTO users (username, password) VALUES (?, ?)', (username, password))
            self.conn.commit()
            QMessageBox.information(self, '注册成功', '用户注册成功，请登录。')
        except sqlite3.IntegrityError:
            QMessageBox.critical(self, '注册失败', '该用户名已被注册，请尝试其他用户名。')

    def closeEvent(self, event):
        self.conn.commit()
        self.cursor.close()
        self.conn.close()
        super().closeEvent(event)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    ex = LoginWindow()
    ex.show()
    sys.exit(app.exec_())
